import redis


class RedisCon:
    def __init__(self):
        self.conn = redis.Redis(host='127.0.0.1', port=6379)

    # 字符串存
    def set_str(self, key, value, time=0):
        # 是否有过期时间
        if time:
            self.conn.setex(key, time, value)  # 以秒为单位
        else:
            self.conn.set(key, value)

    # 字符串取
    def get_str(self, key):
        value = self.conn.get(key)
        # 判断value是否存在
        if value:
            # 转成字符条
            value = str(value, encoding='utf8')
        return value

    def del_str(self, key):
        self.conn.delete(key)


r = RedisCon()
r.set_str('name', 'Mr.Guo')
print(r.get_str('name'))
